﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfCharRain
{
    /// <summary>
    /// CharRainLineControl.xaml 的交互逻辑
    /// </summary>
    public partial class CharRainLineControl : StackPanel
    {
        public StackPanel spFather;
        public MainWindow MainWin;
        public int LightCharCount;
        public int HeaderChar = 0;
        public int CharCount;
        public int Speed;
        public bool IsMoving = false;
        public Random WhichMovingRandom;
        public int WaitTime = 0;
        public CharRainLineControl(MainWindow mw,StackPanel sp, Random WhichMovingRandom)
        {
            this.WhichMovingRandom = WhichMovingRandom;
            spFather = sp;
            MainWin = mw;
            InitializeComponent();
            this.Background = Brushes.Black;
            sp.Children.Add(this); 
            InitMe();
            MainWin.dtGlobal.Tick += TimeUpdate;
            //Timer = new DispatcherTimer();
            //Timer.Tick += new EventHandler(TimeUpdate);
            //InitMe();
            //MainWin.dtGlobal.Tick += new EventHandler(TimeUpdate);
        }
        private void InitMe()
        {
            LightCharCount = Config.GetRandom(Config.ScreenHeight / Config.Font_Size / 4, Config.ScreenHeight / Config.Font_Size / 2);
            CharCount = Config.ScreenHeight / Config.Font_Size + LightCharCount;
            HeaderChar = 0;
            Speed = Config.GetSpeed();
            this.Children.Clear();
            for (int i = 0; i < CharCount; i++)
            {
                TextBlock tb = new TextBlock();
                MyStatck.Children.Add(tb);
                tb.Foreground = Config.ForeColor;
                tb.Opacity = 0;
                tb.FontSize = Config.Font_Size;
                tb.Text = Config.GetGB2312String(1);
            }
            IsMoving = false; 
        }
        private void TimeUpdate(object sender, EventArgs e)
        {
            
            if(IsMoving)
            {
                if (WaitTime > Speed)
                {
                    if (HeaderChar + 1 < CharCount)
                    {
                        for (int i = 0; i < LightCharCount; i++)
                        {
                            if (HeaderChar - LightCharCount >= -i)
                            {
                                (MyStatck.Children[HeaderChar - LightCharCount + i] as TextBlock).Opacity = ((double)i) / ((double)LightCharCount);
                            }
                        }
                        HeaderChar++;
                        (MyStatck.Children[HeaderChar] as TextBlock).Opacity = 100;
                        if (HeaderChar > 1)
                        {

                            (MyStatck.Children[HeaderChar - 1] as TextBlock).Foreground = Config.ForeColor;
                        }
                        (MyStatck.Children[HeaderChar] as TextBlock).Foreground = Config.FirstForeColor;
                    }
                    else
                    {
                        IsMoving = false;
                        InitMe();
                    }
                    WaitTime = 0;
                }
                else
                {
                    WaitTime++;
                }
            }
            else
            {
                if (MainWin.GetMovingNumber() < Config.TotalCout / 5)
                {
                    if (Config.MovingRandom.Next(0, Config.TotalCout) < Config.TotalCout / 5)
                    {
                        IsMoving = true; 
                    }
                }
            }
        }
    }
}
